package org.sn.jdish.example.stack;

/**
 * 栈FILO
 * 
 * @author snzigod@hotmail.com
 *
 */
public class Stack {

	public Node head;
	public Node cur;

	public int size = 0;

	public void push(Object data) {
		if (head == null) {
			head = new Node(data);
			cur = head;
		} else {
			Node node = new Node(data);
			node.pre = cur;
			cur = node;
		}
		size++;
	}

	public Object pop() {
		if (cur == null) {
			return null;
		}
		Node node = cur;
		cur = cur.pre;
		size--;
		return node.data;
	}

	public boolean empty() {
		return size == 0;
	}

	class Node {
		Object data;
		Node pre;

		public Node(Object data) {
			this.data = data;
		}
	}

	public static void main(String[] args) {
		Stack stack = new Stack();
		for (int i = 0; i < 5; i++) {
			stack.push(i);
		}
		while (!stack.empty()) {
			System.out.println(stack.pop());
		}
	}

}
